package com.educhains.web.ctrl.frontend;

import com.alibaba.fastjson.JSONObject;
import com.educhains.module.activity.service.ActivityReplyService;
import com.educhains.module.activity.service.ActivityService;
import com.educhains.module.activity.vo.mb_model.Activity;
import com.educhains.module.activity.vo.mb_model.ActivityJoin;
import com.educhains.module.activity.vo.mb_model.ActivityReply;
import com.educhains.module.activity.vo.mb_model.InfloorActivity;
import com.educhains.module.demo.vo.Page;
import com.educhains.module.user.service.UserService;
import com.educhains.module.user.vo.User;
import com.educhains.web.util.JsonUtils;
import com.educhains.web.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Controller
public class ActivityCtrl {

//    private static final Logger logger = LogManager.getLogger(ActivityCtrl.class);
    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivityReplyService activityReplyService;

    @Autowired
    private UserService userService;

    @RequestMapping("/activityMainPage")
    public String activityMainPage(HttpServletRequest request){
        return "frontend/activity/activityList";
    }

    @RequestMapping("/activityReplyPage")
    public String activityReplyPage(HttpServletRequest request){
        return "frontend/activity/success";
    }

    @RequestMapping("/activityReleasePage")
    public String activityReleasePage( HttpServletRequest request){
        return "frontend/activity/newActivity";
    }

    @RequestMapping("/activityListPage")
    public String activityListPage(HttpServletRequest request){
        return "frontend/activity/activityList";
    }



//    @ResponseBody
    @RequestMapping("/activityMain")
    public String activityMain(HttpServletRequest request) {
        String activityId = request.getParameter("aid");

        Activity activity = activityService.selectByPrimaryKey(activityId);

        List<Activity> activities = activityService.selectActivityListLimit(5, "");

        List<ActivityJoin> activityJoins = activityService.selecetActivityJoinListLimitByAID(activityId, 3);

        List<ActivityReply> activityReplies = activityReplyService.selectActivityReplyList(activityId);


        request.setAttribute("activityReplyList", activityReplies);
        request.setAttribute("activity", activity);
        request.setAttribute("activities", activities);
        request.setAttribute("activityJoins", activityJoins);

//        return returnBody(true, WebUtils.getRequestPath(request, "/activityMain"));
        return "frontend/activity/activityMainPage";
    }


    @RequestMapping(value = "/activityReply" , method = RequestMethod.POST)
    public String activityReply(HttpServletRequest request){
        String activityId = request.getParameter("activity_id");
        String replyContent = request.getParameter("reply_content");
//        String infloorId  = request.getParameter("infloor_id");
        User sessionUser = SessionUtils.getUser(request);


        Date now = new Date();
        Timestamp timeStamp = new Timestamp(now.getTime());
        //FIXME time_id replace acticity_id temply , and the new ActivityReply(...) temply without reply_id
        //FIXME 三级回复格式 回复 用户XX： 手动jsp实现即可
        long time_id = System.currentTimeMillis();

        ActivityReply activityReply = new ActivityReply();
        activityReply.setActivityReplyId(String.valueOf(time_id));
        activityReply.setActivityId(activityId);
        activityReply.setActivityReplyUser(sessionUser.getUserId());
        activityReply.setActivityReplyUserName(sessionUser.getUserName());
        activityReply.setActivityCommitText(replyContent);
        activityReply.setActivityReplyDate(timeStamp);
        activityReply.setActivityReplyIsDelete(false);
        activityReply.setActivityReplyInfloorId(String.valueOf(time_id)); // floorid 取自时间戳

        activityReplyService.createActivityReply(activityReply);

        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 5);
//        return returnBody(true, WebUtils.getRequestPath(request, "/activityReply"));
        List<ActivityReply> activityReplies = activityReplyService.selectActivityReplyList(activityId);

        request.setAttribute("activityReplyList", activityReplies);
        return "frontend/activity/activityMainReplyList";
    }

    @ResponseBody
    @RequestMapping("/activityRelease")
    public String actiRelease(HttpServletRequest request){
        String release_content = request.getParameter("release_content");
        String release_postName = request.getParameter("release_postName");
        String release_pubName = request.getParameter("release_pubName");
        String release_pubPhone = request.getParameter("release_pubPhone");
        String release_pubEmail = request.getParameter("release_pubEmail");
        String release_pubInfo = request.getParameter("release_pubInfo");

        User sessionUser = SessionUtils.getUser(request);


        Date now = new Date();
        //FIXME time_id replace acticity_id temply ,
        long time_id = System.currentTimeMillis();

        Activity activity = new Activity();
        activity.setActivityId(String.valueOf(time_id));

        activity.setActivityPub(sessionUser.getUserId());
        activity.setActivityPubName(release_pubName);
        activity.setActivityPubIntro(release_pubInfo);
        activity.setActivityPubContact(release_pubEmail);
        activity.setActivityPubContactPhone(release_pubPhone);

        activity.setActivityName(release_postName);
        activity.setActivityContentText(release_content);
        activity.setActivityType("讲座");
        activity.setActivityDate(now);
        activity.setActivityIsDelete(false);

        activityService.insertSelective(activity);

        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 100);
//        return returnBody(true, WebUtils.getRequestPath(request, "/activityRelease"));
//        return "redirect:activityList";
        return "true";
    }

    @RequestMapping("/activityList")
    public String activityList(HttpServletRequest request){


        String pageNum = request.getParameter("p") == null ? "1" : request.getParameter("p");
        Page activityPage = activityService.showActivityPage(Integer.valueOf(pageNum));
        request.setAttribute("activityPage", activityPage);
//        return returnBody(true, WebUtils.getRequestPath(request, "/activityReply"));
        return "frontend/activity/activityList";
    }

    @RequestMapping(value = "/activityInfloor", method = RequestMethod.POST)
    public String activityInfloor(HttpServletRequest request){

        String infloor_id  = request.getParameter("infloor_id");

        List<InfloorActivity> infloorActivities = activityReplyService.selectInfloorByFloorId(infloor_id);


        request.setAttribute("infloorActivities", infloorActivities);

        return "frontend/activity/activityMainReplyInfloorList";
    }

    @RequestMapping(value = "/activityInfloorReply", method = RequestMethod.POST)
    public String activityInfloorReply(HttpServletRequest request){

        String infloorContent  = request.getParameter("infloor_reply_content");
        String infloorId  = request.getParameter("infloor");

        User sessionUser = SessionUtils.getUser(request);
        Date now = new Date();

        InfloorActivity infloorActivity = new InfloorActivity();
        infloorActivity.setActivityFloorId(infloorId);
        infloorActivity.setActivityInfloorContent(infloorContent);
        infloorActivity.setActivityInfloorIsDelete(false);
        infloorActivity.setActivityInfloorReplyCreateTime(now);
        infloorActivity.setActivityInfloorReplyPub(sessionUser.getUserId());
        infloorActivity.setActivityInfloorReplyPubName(sessionUser.getUserName());

        activityReplyService.insertInfloorActivitySelective(infloorActivity);

        List<InfloorActivity> infloorActivities = activityReplyService.selectInfloorByFloorId(infloorId);

        request.setAttribute("infloorActivities", infloorActivities);

        return "frontend/activity/activityMainReplyInfloorList";
    }

    @ResponseBody
    @RequestMapping(value = "/activityJoin", method = RequestMethod.GET)
    public String activityJoin(HttpServletRequest request){

        String activityId  = request.getParameter("aid");

        User sessionUser = SessionUtils.getUser(request);
        Date now = new Date();

        ActivityJoin activityJoin = new ActivityJoin();
        activityJoin.setJoinActivityId(activityId);
        activityJoin.setJoinDate(now);
        activityJoin.setJoinUserId(sessionUser.getUserId());
        activityJoin.setJoinUserName(sessionUser.getUserName());
        activityJoin.setJoinUserContribution(sessionUser.getUserContribution());

        try {
            activityService.insertActivityJoin(activityJoin);
        }catch (Exception e) {
            return returnBody(false, "请勿重复参加");
        }

        List<ActivityJoin> activityJoins = activityService.selecetActivityJoinListLimitByAID(activityId, 0);

        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 5);
        request.setAttribute("activityJoins", activityJoins);


        return returnBody(true, "join success");

    }


    private static String returnBody(boolean success, String msg) {
        JSONObject json = new JSONObject();
        if (success) {
            json.put("success", true);
            json.put("msg", msg);
        } else {
            json.put("success", false);
            json.put("msg", msg);
        }
        //return json.toJSONString();
        return JsonUtils.toJSONString(json);
    }
}
